package com.pg.face.pgface.api.service;

import com.pg.face.pgface.api.dto.ProcessInfo;
import com.pg.face.pgface.untils.ErrorCodeEnum;
import com.pg.face.pgface.untils.ImageInfo;
import com.pg.face.pgface.untils.RRException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>className: FaceBiz</p>
 * <p>description: </p>
 *
 * @author
 * @date 2020-01-02 11:25
 */
@Service("com.pg.face.pgface.api.service.FaceBiz")
public class FaceBiz {
    private static Logger logger = LoggerFactory.getLogger(FaceBiz.class);

    @Autowired
    private  FaceEngineServiceImpl faceEngineService;

    public float getSimilarityByTwoFace(ImageInfo imageA, ImageInfo imageB) throws Exception {
        float similarity = 0f;

        Long start = System.currentTimeMillis();
        logger.info("===getSimilarityByTwoFace=1=:{}",start);
        //人脸特征获取
        List<ProcessInfo> processA = faceEngineService.extractFaceFeature(imageA);
        if (processA == null) {
            throw new RRException(ErrorCodeEnum.NO_FACE_DETECTED.getDescription());
        }
        ProcessInfo pInfoA = processA.get(0);

        //人脸特征获取
        List<ProcessInfo> processB = faceEngineService.extractFaceFeature(imageB);
        if (processB == null) {
            throw new RRException(ErrorCodeEnum.NO_FACE_DETECTED.getDescription());
        }
        ProcessInfo pInfoB = processB.get(0);
        logger.info("===getSimilarityByTwoFace=2=:{}",System.currentTimeMillis() - start);

        //人脸比对，获取比对结果
        similarity = faceEngineService.compareFaceFeature(pInfoA.getFaceFeature(), pInfoB.getFaceFeature());

        logger.info("===getSimilarityByTwoFace=3=:{}",System.currentTimeMillis() - start);

        return similarity;
    }
}